package com.tn.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author tn
 * @version 1
 * @ClassName NacosDiscoveryController
 * @description 注册中心
 * @date 2020/8/6 17:53
 */
@RestController
@RequestMapping("discovery")
@Api(tags = "注册中心")
public class NacosDiscoveryController {


    @Autowired
    private RestTemplate restTemplate;

    /**
     * 无论使用哪种注册中心(Eureka,Zookeeper,Consul)，该DiscoveryClient是通用的
     * 它是一个接口
     */
    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 获取 server-user 所有节点
     * @return
     */
    @GetMapping("/test")
    @ApiOperation(value = "注册中心 - test")
    public List<ServiceInstance> findUser() {
        return discoveryClient.getInstances("server-user");
    }

    @GetMapping("/trace")
    @ApiOperation(value = "注册中心 - trace")
    public String trace() {
        return restTemplate.getForObject("http://server-user/find", String.class);
    }


    @GetMapping("/basic")
    @ApiOperation(value = "注册中心 - basic")
    public String basic() {
        return restTemplate.getForObject("http://server-basic/openFeign/test01", String.class);
    }


    @GetMapping("/find")
    @ApiOperation(value = "注册中心 - find")
    public String findStr() {
        return "我是第一";
    }

//    @GetMapping("/find")
//    public String findStr() {
//        return "我是第二";
//    }
}
